<template>
  <div class="main">
    <div style="background-color: white">
      <el-input v-model="role.roleName" placeholder="请输入角色名称"></el-input>
      <el-button
        icon="el-icon-search"
        type="primary"
        circle
        @click="getDate()"
      ></el-button>
      <el-button
        icon="el-icon-refresh-left"
        @click="reset()"
        type="info"
        circle
      ></el-button>
      <el-button
        icon="el-icon-circle-plus-outline"
        type="primary"
        @click="openDialog('add')"
        style="margin-left: 600px"
        circle
      ></el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="角色名" width="180"></el-table-column>
      <el-table-column
        prop="code"
        label="角色编码"
        width="180"
      ></el-table-column>
      <el-table-column prop="desc" label="描述" width="180"></el-table-column>
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <el-popconfirm
            title="您确定删除这条数据么？"
            cancel-button-text="算了算了"
            @confirm="handleDel(scope.row.id)"
          >
            <el-button
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              circle
            ></el-button>
          </el-popconfirm>
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            @click="openDialog('edit', scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <pagination
        :total="total"
        :pageNum="pageNum"
        :pageSize="pageSize"
        @changePageSize="changePageSize"
        @changePageNum="changePageNum"
      ></pagination>
    </div>

    <el-drawer
      :title="formTitle"
      size="30%"
      :visible.sync="drawerDialog"
      :direction="direction"
      :before-close="handleClose"
    >
      <div class="drawerForm">
        <el-form
          :model="roleForm"
          ref="roleForm"
          label-width="180px"
          :rules="rules"
        >
          <div style="margin-top: 100px">
            <el-form-item label="角色名" prop="name">
              <el-input
                v-model="roleForm.name"
                placeholder="请输入角色名"
              ></el-input>
            </el-form-item>
            <el-form-item label="角色编码" prop="code">
              <el-input
                v-model="roleForm.code"
                placeholder="请输入角色编码"
              ></el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="desc">
              <el-input
                v-model="roleForm.desc"
                placeholder="请简单描述一下"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitRoleForm('roleForm')"
                >提交</el-button
              >
              <el-button @click="resetRoleForm()">重置</el-button>
            </el-form-item>
          </div>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { listPage, add, updateById } from "@/api/role";
import Pagination from "@/components/Pagination/index";
export default {
  components: {
    Pagination,
  },
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 10,
      drawerDialog: false,
      formTitle: "",
      direction: "rtl",
      role: {
        roleName: "",
      },
      roleForm: {
        id: "",
        name: "",
        code: "",
        desc: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入角色名", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "长度在 1 到 10 个字符",
            trigger: "blur",
          },
        ],
        code: [
          { required: true, message: "请输入角色编码", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    changePageSize(val) {
      this.pageSize = val;
      this.getDate();
    },
    changePageNum(val) {
      this.pageNum = val;
      this.getDate();
    },
    getDate() {
      console.log(this.role.roleName);
      const param = {
        page: this.pageNum,
        limit: this.pageSize,
        roleName: this.role.roleName,
      };
      listPage(param).then((res) => {
        console.log(res.data);
        this.total = res.total;
        this.tableData = res.data;
      });
    },
    reset() {
      this.role.roleName = "";
      this.getDate();
    },
    delete() {},
    handleClose() {
      this.drawerDialog = false;
    },
    openDialog(name, data) {
      if (name == "add") {
        this.formTitle = "添加";
      } else if (name == "edit") {
        console.log(data);
        this.formTitle = "编辑";
        this.roleForm = {
          id: data.id,
          code: data.code,
          name: data.name,
          desc: data.desc,
        };
      }
      this.drawerDialog = true;
    },
    resetRoleForm() {
      this.roleForm = {
        id: "",
        name: "",
        desc: "",
      };
    },
    submitRoleForm(formName) {
      if (this.formTitle == "添加") {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            add(this.roleForm).then((res) => {
              if (res.code == 0) {
                this.$message({
                  message: "添加成功",
                  type: "success",
                });
                this.drawerDialog = false;
                this.getDate();
              } else {
                this.$message({
                  message: "添加失败",
                  type: "error",
                });
              }
            });
          }
        });
      } else if (this.formTitle == "编辑") {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm("此操作将修改此用户信息, 是否继续?", "提示", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }).then(() => {
              updateById(this.roleForm).then((res) => {
                if (res.code == 0) {
                  this.$message({
                    message: "修改成功",
                    type: "success",
                  });
                  this.getDate();
                  this.drawerDialog = false;
                  this.resetRoleForm();
                } else {
                  this.$message({
                    message: "修改失败",
                    type: "error",
                  });
                }
              });
            });
          }
        });
      }
    },
    handleDel(id) {
      const role = {
        id: id,
        isDeleted: 1,
      };
      updateById(role).then((res) => {
        if (res.code == 0) {
          this.$message({
            showClose: true,
            message: "删除成功",
            type: "success",
          });
          this.getDate();
        } else {
          this.$message({
            showClose: true,
            message: "删除失败",
            type: "erorr",
          });
        }
      });
    },
  },
  mounted() {
    this.getDate();
  },
};
</script>
<style lang="scss" scoped>
.el-input {
  width: 150px;
  padding: 10px;
}
.el-select {
  width: 150px;
}
.el-table-column {
  text-align: center;
}
.main {
  margin-left: 30px;
}
.pagination {
  margin-top: 30px;
  text-align: center;
}
.el-input {
  width: 200px;
}
.el-textarea {
  width: 200px;
}
.el-select {
  width: 200px;
}
</style>
